package com.wibo.account;

import java.util.ArrayList;
import java.util.List;

import weibo4android.IDs;
import weibo4android.User;
import weibo4android.Weibo;
import weibo4android.WeiboException;

import com.wibo.common.FileDbHelper;
import com.wibo.common.HScrollViewGroup;
import com.wibo.common.UserItem;
import com.wibo.common.Utility;
import com.wibo.common.HScrollViewGroup.OnItemSelectedListener;
import com.wibo.service.IWeiboService;
import com.wibo.service.IWeiboServiceCallback;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;

public class UserListActivity extends Activity implements OnItemClickListener,
		OnClickListener, OnItemSelectedListener, OnScrollListener {

	public static final String TAG = "UserListActivity";
	private static Weibo mWeibo = null;
	private static OAuthConstant mAuthConstant = null;
	protected FileDbHelper mDbHelper = null;
	
	CUserManager mUserManager = null;
	private boolean mbRunning = false;
	
	private long mUserId = -1;
	private String mUserName = "";
	
	private HScrollViewGroup mListContentView;
	
	private LinearLayout mLoadingLayout;
	private TextView mNoResultText; 
	
//	private RelativeLayout mMoreWeiboLayout;
//	private ProgressBar mMoreWeiboProgress;
//	
//	private RelativeLayout mMoreWeiboLayout2;
//	private ProgressBar mMoreWeiboProgress2;
	
	
	private View mLvfooter = null;
	private ProgressBar mFooterPro = null;
	
	private View mLvfooter2 = null;
	private ProgressBar mFooterPro2 = null;
	
	private FrameLayout mFrameLayout03;
	private RelativeLayout mBarLayout;
	
	private TextView mTextView01 = null;
	private TextView mTextView02 = null;
	
	private ListView mList01;
	private ListView mList02;
	
	private ArrayList<UserItem> mListData01 = null;
	private ArrayList<UserItem> mListData02 = null;
	
	private UserListItemAdapter mListAdpapter01;
	private UserListItemAdapter mListAdpapter02;
	
	private int mCursor01 = 0;
	private int mCursor02 = 0;
	
	private static final int EVERY_SIZE = 20;
	
	private int mCurrentScreen = 0;
	
	private int mUserType = USER_TYPE_ME;
	
	private static final int USER_TYPE_ME = 0;
	private static final int USER_TYPE_OTHERS = 1;
	
	private static final int GET_LIST01_FINISHED = 0;
	private static final int GET_LIST01_ERROR = 1;
	private static final int GET_LIST02_FINISHED = 2;
	private static final int GET_LIST02_ERROR = 3;
	private static final int GET_LIST01_PIC_FINISHED = 4;
	private static final int GET_LIST02_PIC_FINISHED = 5;
	
	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (!mbRunning) {
				return;
			}
			switch (msg.what) {
			case GET_LIST01_FINISHED:
				mLoadingLayout.setVisibility(LinearLayout.GONE);
				if (msg.obj != null) {
					ArrayList<UserItem> dataList = (ArrayList<UserItem>)msg.obj;
					mListData01.addAll(mListData01.size(), dataList);
					mCursor01 += dataList.size();
					
					mListAdpapter01.notifyDataSetChanged();
					loadWeiboPicList1(mLastFreshImageIndexList1, DOWNLOAD_IMAGE_MAX_SIZE);
				}
				
				if (mListData01.size() > 0) {
					mList01.setVisibility(ListView.VISIBLE);
					mLvfooter.setVisibility(View.VISIBLE);
					mNoResultText.setVisibility(View.GONE);
				}else{
					mList01.setVisibility(ListView.INVISIBLE);
					mNoResultText.setVisibility(View.VISIBLE);
				}
				mFooterPro.setVisibility(ProgressBar.INVISIBLE);
				
				break;
			case GET_LIST01_ERROR:
				mLoadingLayout.setVisibility(LinearLayout.GONE);
				Toast.makeText(UserListActivity.this, getString(R.string.getFailed), Toast.LENGTH_SHORT).show();
				if (mListData01.size() > 0) {
					mList01.setVisibility(ListView.VISIBLE);
					mLvfooter.setVisibility(View.VISIBLE);
					mNoResultText.setVisibility(View.GONE);
				}else{
					mList01.setVisibility(ListView.INVISIBLE);
					mNoResultText.setVisibility(View.VISIBLE);
				}
				mFooterPro.setVisibility(ProgressBar.INVISIBLE);
				break;
			case GET_LIST02_FINISHED:
				mLoadingLayout.setVisibility(LinearLayout.GONE);
				if (msg.obj != null) {
					ArrayList<UserItem> dataList = (ArrayList<UserItem>)msg.obj;
					mListData02.addAll(mListData02.size(), dataList);
					mCursor02 += dataList.size();
					
					mListAdpapter02.notifyDataSetChanged();
					loadWeiboPicList2(mLastFreshImageIndexList2, DOWNLOAD_IMAGE_MAX_SIZE);
				}
				if (mListData02.size() > 0) {
					mList02.setVisibility(ListView.VISIBLE);
					mLvfooter2.setVisibility(View.VISIBLE);
					mNoResultText.setVisibility(View.GONE);
				}else{
					mList02.setVisibility(ListView.INVISIBLE);
					mNoResultText.setVisibility(View.VISIBLE);
				}
				mFooterPro2.setVisibility(ProgressBar.INVISIBLE);
				break;
			case GET_LIST02_ERROR:
				mLoadingLayout.setVisibility(LinearLayout.GONE);
				Toast.makeText(UserListActivity.this, getString(R.string.getFailed), Toast.LENGTH_SHORT).show();
				if (mListData02.size() > 0) {
					mList02.setVisibility(ListView.VISIBLE);
					mLvfooter2.setVisibility(View.VISIBLE);
					mNoResultText.setVisibility(View.GONE);
				}else{
					mList01.setVisibility(ListView.INVISIBLE);
					mNoResultText.setVisibility(View.VISIBLE);
				}
				mFooterPro2.setVisibility(ProgressBar.INVISIBLE);
				break;
			case GET_LIST01_PIC_FINISHED:
				mListAdpapter01.notifyDataSetChanged();
				break;
			case GET_LIST02_PIC_FINISHED:
				mListAdpapter02.notifyDataSetChanged();
				break;
			}
		}
	};
	
	private void sendMessage(int what, Object obj) {
		Message msg = mHandler.obtainMessage(what, obj);
		mHandler.sendMessage(msg);
	}
	
	private void sendMessage(int what, int arg1, int arg2, Object obj) {
		Message msg = mHandler.obtainMessage(what, arg1, arg2, obj);
		mHandler.sendMessage(msg);
	}
	
	private IWeiboService mWeiboService = null;
	
	private ServiceConnection mAccountConnection = new ServiceConnection() {
		
		public void onServiceDisconnected(ComponentName name) {
			mWeiboService = null;
			
		}
		
		public void onServiceConnected(ComponentName name, IBinder service) {
			mWeiboService = IWeiboService.Stub.asInterface(service);
			try{
				mWeiboService.registerCallback(mWeiboServiceCallback);
			}catch(RemoteException e){
				e.printStackTrace();
			}
			
		}
	};
	
	private IWeiboServiceCallback.Stub mWeiboServiceCallback = new IWeiboServiceCallback.Stub() {
		
		@Override
		public void unreadWeibo(long count) throws RemoteException {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void unreadTransmit(long count) throws RemoteException {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void unreadFans(long count) throws RemoteException {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void unreadComment(long count) throws RemoteException {
		}

		@Override
		public void haveNewWeibo(boolean isHave) throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void unreadTotalMessages(long count) throws RemoteException {
			// TODO Auto-generated method stub
			
		}
	};
	
	private boolean bindWeiboService() {
		Intent serviceIntent = new Intent();
		serviceIntent.setAction("com.wibo.service.WeiboService");
		ComponentName name = getApplicationContext().startService(serviceIntent);
		if (name == null) {
			return false;
		}
		boolean isSuc = getApplicationContext().bindService(serviceIntent, mAccountConnection, Context.BIND_AUTO_CREATE);
		
		return isSuc;
	}

	private void setContentViewSkin() {
		int skin = mUserManager.getMySkin();
		switch (skin) {
		case 0:
			setContentView(R.layout.userlist);
			break;
		case 1:
			setContentView(R.layout.userlist_2);
			break;
		default:
			setContentView(R.layout.userlist);
			break;
		}
	}
	
	private int getListFooterSkin() {
		int skin = mUserManager.getMySkin();
		switch (skin) {
		case 0:
			return R.layout.userlistfooter;
		case 1:
			return R.layout.userlistfooter_2;
		default:
			return R.layout.userlistfooter;
		}
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		//setContentView(R.layout.userlist);
		
		mbRunning = true;
		mUserManager = CUserManager.getInstance(this);
		setContentViewSkin();
		
		if (mAuthConstant == null)
			mAuthConstant = OAuthConstant.getInstance();

		if (mWeibo == null)
			mWeibo = mAuthConstant.getWeibo();

		if (mDbHelper == null)
			mDbHelper = new FileDbHelper(this);
		
		mListContentView = (HScrollViewGroup) findViewById(R.id.userListContent);
		mListContentView.setOnItemSelectedListener(this);
		
		mTextView01 = (TextView) findViewById(R.id.Text01);
		mTextView02 = (TextView) findViewById(R.id.Text02);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			bindWeiboService();
			
			mUserId = extras.getLong("userId");
			mUserName = extras.getString("userName");
			int type = extras.getInt("type");
			setTitle(mUserName);
			
			initHeaderBar();
			
			mLoadingLayout = (LinearLayout) findViewById(R.id.LoadingLayout);
			mNoResultText = (TextView) findViewById(R.id.NoResultText);
			
			mList01 = (ListView) findViewById(R.id.List01);
			mList01.setVisibility(ListView.INVISIBLE);
			mList02 = (ListView) findViewById(R.id.List02);
			mList02.setVisibility(ListView.INVISIBLE);
			mLvfooter = LayoutInflater.from(UserListActivity.this).inflate(getListFooterSkin(), null);	
			
			if(mLvfooter != null){
				mFooterPro = (ProgressBar) mLvfooter.findViewById(R.id.moreWeiboProgress);
				mFooterPro.setVisibility(ProgressBar.INVISIBLE);
				mLvfooter.setVisibility(View.INVISIBLE);

				mList01.addFooterView(mLvfooter);
				mList01.setFooterDividersEnabled(true);
				mLvfooter.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						//Toast.makeText(WiboMainActivity.this, "~~~~", Toast.LENGTH_SHORT).show();
						if (!Utility.isNetworkAvailable(UserListActivity.this)){
							Toast.makeText(UserListActivity.this, getString(R.string.networkFail), Toast.LENGTH_SHORT).show();
							return;
						}
						getList01Thread(mUserId);
					}
				});
			}
			
			mLvfooter2 = LayoutInflater.from(UserListActivity.this).inflate(getListFooterSkin(), null);	
			
			if(mLvfooter2 != null){
				mFooterPro2 = (ProgressBar) mLvfooter2.findViewById(R.id.moreWeiboProgress);
				mFooterPro2.setVisibility(ProgressBar.INVISIBLE);
				mLvfooter2.setVisibility(View.INVISIBLE);

				mList02.addFooterView(mLvfooter2);
				mList02.setFooterDividersEnabled(true);
				mLvfooter2.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						if (!Utility.isNetworkAvailable(UserListActivity.this)){
							Toast.makeText(UserListActivity.this, getString(R.string.networkFail), Toast.LENGTH_SHORT).show();
							return;
						}
						
						getList02Thread(mUserId);
					}
				});
			}
			
			mBarLayout = (RelativeLayout) findViewById(R.id.scrollbarTipLayout02);
			mBarLayout.setVisibility(RelativeLayout.VISIBLE);
			mTextView01.setText(getString(R.string.focusTitle));
			mTextView02.setText(getString(R.string.fansTitle));
			mListData01 = new ArrayList<UserItem>();
			mListData02 = new ArrayList<UserItem>();
			mListAdpapter01 = new UserListItemAdapter(this);
			mListAdpapter01.setmItemData(mListData01);
			
			mListAdpapter02 = new UserListItemAdapter(this);
			mListAdpapter02.setmItemData(mListData02);
			
			mList01.setAdapter(mListAdpapter01);
			mList02.setAdapter(mListAdpapter02);
			mList01.setOnItemClickListener(this);
			mList02.setOnItemClickListener(this);
			mList01.setOnScrollListener(this);
			mList02.setOnScrollListener(this);

		
			mCurrentScreen = 0;
			setUiShow();
			if (mUserId != mUserManager.getAccountUser().getId()) {
				mUserType = USER_TYPE_OTHERS;
			}
			if (type == 0) {
				//onSelected(type);
				getList01Thread(mUserId);
			}else{
				onSelected(1);
				//getList02Thread(mUserId);
			}
			
		}
	}
	
	private void initHeaderBar(){
  	  
        FrameLayout selectLayout = (FrameLayout)findViewById(R.id.TitleHelpLayout01);
        selectLayout.setSelected(true);
    
        selectLayout = (FrameLayout)findViewById(R.id.TitleHelpLayout02);
        selectLayout.setSelected(true);
     
        selectLayout = (FrameLayout)findViewById(R.id.TitleHelpLayout03);
        selectLayout.setSelected(true);

        Button btnAllTop = (Button) findViewById(R.id.Btn01);
        Button btnFreeTop = (Button) findViewById(R.id.Btn02);
        Button btnPayTop = (Button) findViewById(R.id.Btn03);
        
        btnAllTop.setOnClickListener(this);
        btnFreeTop.setOnClickListener(this);
        btnPayTop.setOnClickListener(this);
        
    }
	
	private void setUiShow(){
		if (mCurrentScreen == 0){
			mTextView01.setSelected(true);
			mTextView02.setSelected(false);
		}else{
			mTextView01.setSelected(false);
			mTextView02.setSelected(true);
		}
	}
	
	private Thread mGetList01Thread = null;
	
	private void getList01Thread(final long userId) {
		if (mGetList01Thread != null) {
			if (mGetList01Thread.isAlive()) {
				mGetList01Thread.interrupt();
			}
			mGetList01Thread = null;
		}
		
		mGetList01Thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					if (mUserManager.getMyFocusIds() == null) {
						IDs ids = mWeibo.getFriendsIDSByUserId(String.valueOf(mUserManager.getUserId()), 0);
						if (ids != null) {
							mUserManager.setMyFocusIds(ids);
						}else{
							sendMessage(GET_LIST01_ERROR, null);
							return;
						}
					}
					List<User> users = mWeibo.getFriendsStatuses(String.valueOf(userId), mCursor01, EVERY_SIZE);
					
					if (users != null && users.size() > 0) {
						ArrayList<UserItem> userItemData = new ArrayList<UserItem>();
						
						for (User user : users) {
							UserItem userItem = new UserItem();
							userItem.setmFollowersCount(user.getFollowersCount());
							userItem.setmWeiboUserPic(user.getProfileImageURL().toString());
							userItem.setmScreenName(user.getScreenName());
							userItem.setmLocation(user.getLocation());
							userItem.setmDomain(user.getUserDomain());
							userItem.setmStatusesCount(user.getStatusesCount());
							userItem.setmFavouritesCount(user.getFavouritesCount());
							userItem.setmFriendsCount(user.getFriendsCount());
							userItem.setmUserName(user.getName());
							userItem.setmCreateTime(Utility.formatDate(user.getCreatedAt()));
							userItem.setmUrl(user.getURL() == null ? null : user.getURL().toString());
							userItem.setmDescription(user.getDescription());
							userItem.setmUserId(user.getId());
							userItem.setmSex(user.getGender());
							//userItem.setmUserType((int)userCursor.getLong(add_data1));
							
							IDs ids = mUserManager.getMyFocusIds();
							for (int i = 0; i < ids.getIDs().length; i++) {
								if (user.getId() == ids.getIDs()[i]) {
									userItem.setmIsFocus(true);
									break;
								}
							}
							//userItem.setmIsFollower(false);
							userItem.setmVLogo(user.isVerified());
							if (Utility.isImageExist(UserListActivity.this, user.getProfileImageURL().toString(), Utility.WEIBO_PIC, Utility.USER_PIC)) {
								userItem.setmIsLoadHeadPic(true);
							}
							userItemData.add(userItem);
						}
						
						sendMessage(GET_LIST01_FINISHED, userItemData);
					}
				} catch (WeiboException e) {
					e.printStackTrace();
					sendMessage(GET_LIST01_ERROR, null);
				}
			}
		});
		if (mListData01.size() == 0) {
			mLoadingLayout.setVisibility(LinearLayout.VISIBLE);
		}
		mFooterPro.setVisibility(View.VISIBLE);
		mGetList01Thread.start();
	}
	
	private Thread mGetList02Thread = null;
	
	private void getList02Thread(final long userId) {
		if (mGetList02Thread != null) {
			if (mGetList02Thread.isAlive()) {
				mGetList02Thread.interrupt();
			}
			mGetList02Thread = null;
		}
		
		mGetList02Thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					if (mUserManager.getMyFocusIds() == null) {
						IDs ids = mWeibo.getFriendsIDSByUserId(String.valueOf(mUserManager.getUserId()), 0);
						if (ids != null) {
							mUserManager.setMyFocusIds(ids);
						}else{
							sendMessage(GET_LIST02_ERROR, null);
							return;
						}
					}
					
					List<User> users = mWeibo.getFollowersStatuses(String.valueOf(userId), mCursor02, EVERY_SIZE);
					
					if (users != null && users.size() > 0) {
						ArrayList<UserItem> userItemData = new ArrayList<UserItem>();
						
						for (User user : users) {
							UserItem userItem = new UserItem();
							userItem.setmFollowersCount(user.getFollowersCount());
							userItem.setmWeiboUserPic(user.getProfileImageURL().toString());
							userItem.setmScreenName(user.getScreenName());
							userItem.setmLocation(user.getLocation());
							userItem.setmDomain(user.getUserDomain());
							userItem.setmStatusesCount(user.getStatusesCount());
							userItem.setmFavouritesCount(user.getFavouritesCount());
							userItem.setmFriendsCount(user.getFriendsCount());
							userItem.setmUserName(user.getName());
							userItem.setmCreateTime(Utility.formatDate(user.getCreatedAt()));
							userItem.setmUrl(user.getURL() == null ? null : user.getURL().toString());
							userItem.setmDescription(user.getDescription());
							userItem.setmUserId(user.getId());
							userItem.setmSex(user.getGender());
							IDs ids = mUserManager.getMyFocusIds();
							for (int i = 0; i < ids.getIDs().length; i++) {
								if (user.getId() == ids.getIDs()[i]) {
									userItem.setmIsFocus(true);
									break;
								}
							}
							//userItem.setmUserType((int)userCursor.getLong(add_data1));
							userItem.setmVLogo(user.isVerified());
							if (Utility.isImageExist(UserListActivity.this, user.getProfileImageURL().toString(), Utility.WEIBO_PIC, Utility.USER_PIC)) {
								userItem.setmIsLoadHeadPic(true);
							}
							userItemData.add(userItem);
						}
						
						if (mListData01.size() == 0) {
							try {
								if (mWeiboService != null) {
									mWeiboService.resetCount(4);
								}
							} catch (RemoteException e) {
								e.printStackTrace();
							}
						}
						sendMessage(GET_LIST02_FINISHED, userItemData);
					}
				} catch (WeiboException e) {
					e.printStackTrace();
					sendMessage(GET_LIST02_ERROR, null);
				}
			}
		});
		if (mListData02.size() == 0) {
			mLoadingLayout.setVisibility(LinearLayout.VISIBLE);
		}
		mFooterPro2.setVisibility(View.VISIBLE);
		mGetList02Thread.start();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mbRunning = false;
		if (mGetList01Thread != null) {
			if (mGetList01Thread.isAlive()) {
				mGetList01Thread.interrupt();
			}
			mGetList01Thread = null;
		}
		if (mGetList02Thread != null) {
			if (mGetList02Thread.isAlive()) {
				mGetList02Thread.interrupt();
			}
			mGetList02Thread = null;
		}
		
		if(mWeiboService != null){
			try{
				getApplicationContext().unbindService(mAccountConnection);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				mWeiboService = null;
			}
		}
	}
	
	

	/* (non-Javadoc)
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		switch (arg0.getId()) {
		case R.id.List01:
			Intent intent = new Intent(this, WiboUserDetailsActivity.class);
			Bundle bundle = new Bundle();
    		bundle.putLong("userId", mListData01.get(arg2).getmUserId());
    		bundle.putSerializable("userItem", mListData01.get(arg2));
    		intent.putExtras(bundle);
			startActivity(intent);
			break;
		case R.id.List02:
			intent = new Intent(this, WiboUserDetailsActivity.class);
			bundle = new Bundle();
    		bundle.putLong("userId", mListData02.get(arg2).getmUserId());
    		bundle.putSerializable("userItem", mListData02.get(arg2));
    		intent.putExtras(bundle);
			startActivity(intent);
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Btn01:
			if(mCurrentScreen != 0){
				mLoadingLayout.setVisibility(LinearLayout.GONE);
				mNoResultText.setVisibility(View.GONE);
				if (mCurrentScreen == 1){
					//创建Animation对象  
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), R.anim.total_2_21);  
					animation.setFillAfter(true);
					//组件播放动画  
					mBarLayout.startAnimation(animation); 
				}else{
					//创建Animation对象  
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), R.anim.total_2_12);  
					animation.setFillAfter(true);
					//组件播放动画  
					mBarLayout.startAnimation(animation); 
				}
				
				SetTypeLayoutVisible(0);
				if (mListData01.size() == 0) {
					getList01Thread(mUserId);
				}
				setUiShow();
			}
			break;
		case R.id.Btn02:
			if(mCurrentScreen != 1){
				mLoadingLayout.setVisibility(LinearLayout.GONE);
				mNoResultText.setVisibility(View.GONE);
				if (mCurrentScreen == 0){
					//创建Animation对象  
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), R.anim.total_2_12);  
					animation.setFillAfter(true);
					//组件播放动画  
					mBarLayout.startAnimation(animation); 
				}else{
					//创建Animation对象  
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), R.anim.total_2_21);  
					animation.setFillAfter(true);
					//组件播放动画  
					mBarLayout.startAnimation(animation); 
				}
				SetTypeLayoutVisible(1);

				if (mListData02.size() == 0) {
					getList02Thread(mUserId);
				}
				setUiShow();
			}
			break;
		}
	}

	@Override
	public void onSelected(int selectedIndex) {
		switch (selectedIndex) {
		case 0:
			if(mCurrentScreen != 0){
				mLoadingLayout.setVisibility(LinearLayout.GONE);
				mNoResultText.setVisibility(View.GONE);
				if (mCurrentScreen == 1){
					//创建Animation对象  
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), R.anim.total_2_21);  
					animation.setFillAfter(true);
					//组件播放动画  
					mBarLayout.startAnimation(animation); 
				}else{
					//创建Animation对象  
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), R.anim.total_2_12);  
					animation.setFillAfter(true);
					//组件播放动画  
					mBarLayout.startAnimation(animation); 
				}
				
				SetTypeLayoutVisible(0);
				if (mListData01.size() == 0) {
					getList01Thread(mUserId);
				}
				setUiShow();
			}
			break;
		case 1:
			if(mCurrentScreen != 1){
				mLoadingLayout.setVisibility(LinearLayout.GONE);
				mNoResultText.setVisibility(View.GONE);
				if (mCurrentScreen == 0){
					//创建Animation对象  
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), R.anim.total_2_12);  
					animation.setFillAfter(true);
					//组件播放动画  
					mBarLayout.startAnimation(animation); 
				}else{
					//创建Animation对象  
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), R.anim.total_2_21);  
					animation.setFillAfter(true);
					//组件播放动画  
					mBarLayout.startAnimation(animation); 
				}
				SetTypeLayoutVisible(1);

				if (mListData02.size() == 0) {
					getList02Thread(mUserId);
				}
				setUiShow();
			}
			break;
		}		
	}
	
	// 设置哪个列表头需要显示,同时清空临时数据
    private void SetTypeLayoutVisible(int type){
    	
    	// 清除临时数据
		mCurrentScreen = type;
		
        switch(type){
    	case 0:
    		mListContentView.showScreen(0);
    		mList01.setVisibility(View.VISIBLE);
    		mList02.setVisibility(View.INVISIBLE);
			break;
    	case 1:
    		mListContentView.showScreen(1);
    		mList01.setVisibility(View.INVISIBLE);
    		mList02.setVisibility(View.VISIBLE);
    		break;
    	}
    }
	
	private Thread mGetPicThreadList1 = null;
	
	private void loadWeiboPicList1(final int startIndex, final int lastIndex) {
		
		if (mGetPicThreadList1 != null) {
			if (mGetPicThreadList1.isAlive()) {
				mGetPicThreadList1.interrupt();
			}
			mGetPicThreadList1 = null;
		}
		
		mGetPicThreadList1 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
				int size = mListData01.size();
				String path = null;
				for (int i = startIndex; lastIndex < size && i <= lastIndex; i++) {
					path = Utility.getImagePath(UserListActivity.this, mListData01.get(i).getmWeiboUserPic(), Utility.WEIBO_PIC, Utility.USER_PIC);
					if (path != null) {
						mListData01.get(i).setmIsLoadHeadPic(true);
					}
					
					sendMessage(GET_LIST01_PIC_FINISHED, null);
				}
			}
		});
		
		mGetPicThreadList1.start();
		
	}
	
	private Thread mGetPicThreadList2 = null;
	
	private void loadWeiboPicList2(final int startIndex, final int lastIndex) {
		
		if (mGetPicThreadList2 != null) {
			if (mGetPicThreadList2.isAlive()) {
				mGetPicThreadList2.interrupt();
			}
			mGetPicThreadList2 = null;
		}
		
		mGetPicThreadList2 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
				int size = mListData02.size();
				String path = null;
				for (int i = startIndex; lastIndex < size && i <= lastIndex; i++) {
					path = Utility.getImagePath(UserListActivity.this, mListData02.get(i).getmWeiboUserPic(), Utility.WEIBO_PIC, Utility.USER_PIC);
					if (path != null) {
						mListData02.get(i).setmIsLoadHeadPic(true);
					}
										
					sendMessage(GET_LIST02_PIC_FINISHED, null);
				}
			}
		});
		
		mGetPicThreadList2.start();
		
	}
	
	public static final int DOWNLOAD_IMAGE_MAX_SIZE = 10;
	// 记录上次获取图片信息的时间
	private int mLastFreshImageIndexList1 = 0;
	private int mLastFreshImageIndexList2 = 0;
	
	private void refreshImageForList1(int viewId){
		if(mListData01 != null && mListData01.size() > 0){
			int startIndex = mList01.getFirstVisiblePosition();
			int endIndex = mList01.getLastVisiblePosition();
			
			// 设置上一次的刷新位置
			mLastFreshImageIndexList1 = startIndex;
			
			if(startIndex > 0){
				--startIndex;
			}
			if(endIndex >= mListData01.size()){
				endIndex = mListData01.size() - 1;
			}
			
			for (int i = startIndex; i <= endIndex; i++) {
				if (!mListData01.get(i).ismIsLoadHeadPic()){
					loadWeiboPicList1(i, endIndex);
					return;
				}
			}
		}
	}
	
	private void refreshImageForList2(int viewId){
		if(mListData02 != null && mListData02.size() > 0){
			int startIndex = mList02.getFirstVisiblePosition();
			int endIndex = mList02.getLastVisiblePosition();
			
			// 设置上一次的刷新位置
			mLastFreshImageIndexList2 = startIndex;
			
			if(startIndex > 0){
				--startIndex;
			}
			if(endIndex >= mListData02.size()){
				endIndex = mListData02.size() - 1;
			}
			
			for (int i = startIndex; i <= endIndex; i++) {
				if (!mListData02.get(i).ismIsLoadHeadPic()){
					loadWeiboPicList2(i, endIndex);
					return;
				}
			}
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		switch (view.getId()) {
		case R.id.List01:
			if(Math.abs(firstVisibleItem - mLastFreshImageIndexList1) >= DOWNLOAD_IMAGE_MAX_SIZE){
				refreshImageForList1(view.getId());
			}
			break;
		case R.id.List02:
			if(Math.abs(firstVisibleItem - mLastFreshImageIndexList2) >= DOWNLOAD_IMAGE_MAX_SIZE){
				refreshImageForList2(view.getId());
			}
			break;
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if(scrollState == SCROLL_STATE_IDLE){
			switch (view.getId()) {
			case R.id.List01:
				refreshImageForList1(view.getId());
				break;
			case R.id.List02:
				refreshImageForList2(view.getId());
				break;
			}
		}
	}

}
